package org.crawler;

import org.crawler.task.CarIdNameMap;
import org.crawler.task.DataRecQueue;
import org.crawler.task.FreshUrlMap;
import org.crawler.task.FreshUrlQueue;
import org.crawler.task.HTMLTaskQueue;
import org.crawler.task.ImageQueue;
import org.crawler.task.JsonStrTaskQueue;
import org.crawler.task.TaskCenter;
import org.crawler.task.WebUrlQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class Counter extends Thread {
	
	@Autowired
	private HTMLTaskQueue htmlpage;
	
	@Autowired
	private WebUrlQueue webUrl;
	
	@Autowired
	private FreshUrlQueue freshUrl;
	
	@Autowired
	private JsonStrTaskQueue record;
	
	@Autowired
	private FreshUrlMap freshUrlMap;
	
	@Autowired
	private DataRecQueue dataRecQueue;
	
	@Autowired
	private CarIdNameMap carIdNameMap;
	
	@Autowired
	private ImageQueue imageQueue;
	
	
	public static long nFetchedUrl=0;
	public static long nExtractUrl=0;
	public static long nWriteStr=0;
	public static long nExtractData=0;
	public static long nFailedUrl=0;
	
	private boolean needStop;
	
	public boolean isNeedStop() {
		return needStop;
	}

	public void setNeedStop(boolean needStop) {
		this.needStop = needStop;
	}

	public Counter()
	{
		needStop=false;
	}
	
	public void run()
	{
		while(true)
		{
			
			showStatic();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public boolean isTaskClear()
	{
		if(htmlpage.getTaskCount()==0 &&
				webUrl.getTaskCount()==0 &&
						freshUrlMap.getFreshUrlMap().size()==0 &&
				dataRecQueue.getTaskCount()==0 &&
						record.getTaskCount()==0 &&
						carIdNameMap.getBandIdMap().size()==0 &&
						carIdNameMap.getSubBandIdMap().size()==0 &&
						carIdNameMap.getSeriesIdMap().size()==0 &&
						imageQueue.getTaskQueue().size()==0)
		return true;
		return false;
	}
	
	public void showStatic()
	{
		System.out.println("------------------------------------------------------------------------------------------");
		System.out.println("[Counter] Append:  htmlpage:"+htmlpage.getTaskCount()+"  webUrl:"+webUrl.getTaskCount()+"  freshUrlMap:"+freshUrlMap.getFreshUrlMap().size()+"  dataRecQueue:"+dataRecQueue.getTaskCount()+"  LineStr:"+record.getTaskCount()
				+" CarKVPair:"+(carIdNameMap.getBandIdMap().size()+carIdNameMap.getSubBandIdMap().size()+carIdNameMap.getSeriesIdMap().size())+" imageQueue"+imageQueue.getTaskCount());
		System.out.println("[Counter] Done:    fetchedUrl:"+nFetchedUrl+"  newUrl:"+nExtractUrl+"  fetchedData:"+nExtractData+"   writeLine:"+nWriteStr+ "  failedUrl:"+nFailedUrl);
        System.out.println("  ");;

		
	}
}
